<?php

namespace App\Jobs;

use App\Models\WxSyncRelation;
use App\Wen\Utils\Utils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class CreatePostSyncJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $uid;
    public $contents;
    public $circle_id;
    public $tags;
    public $goods;
    public $image_urls;
    public $video_urls;
    public $address;
    public $videoThumbUrl;
    public $video_width;
    public $video_height;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($id, $uid, $contents, $circle_id, $tags, $goods, $image_urls, $video_urls, $address, $videoThumbUrl, $video_width, $video_height)
    {
        //
        $this->id = $id;
        $this->uid = $uid;
        $this->contents = $contents;
        $this->circle_id = $circle_id;
        $this->tags = $tags;
        $this->goods = $goods;
        $this->image_urls = $image_urls;
        $this->video_urls = $video_urls;
        $this->address = $address;
        $this->videoThumbUrl = $videoThumbUrl;
        $this->video_width = $video_width;
        $this->video_height = $video_height;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        return ;
        $syncCmsUrls = Utils::syncCmsUrls($this->uid , $this->circle_id, 0);
        if(_empty_($syncCmsUrls)){
            Log::alert('syncCmsUrls is null');
        }else{
            $origin_data = [
                'id' => $this->id,
                'uid' => $this->uid,
                'contents' => $this->contents,
                'circle_id' => $this->circle_id,
                'tags' => $this->tags,
                'goods' => $this->goods,
                'image_urls' => $this->image_urls,
                'video_urls' => $this->video_urls,
                'address' => $this->address,
                'videoThumbUrl' => $this->videoThumbUrl,
                'video_width' => $this->video_width,
                'video_height' => $this->video_height,
            ];


            foreach ($syncCmsUrls as $sync){
                $data = ['origin_data'=> $origin_data, 'publish_data'=>$sync ];
                $host = parse_url($sync['url'], PHP_URL_HOST);
                $response = Http::asForm()->post($sync['url'], $data);
                if($response->successful()){
                    $res_arr = $response->json();
                    if($res_arr){
                        if($res_arr['code'] == 1){
                            $WxSyncRelation = new WxSyncRelation();
                            $WxSyncRelation->domain = $host;
                            $WxSyncRelation->type = 'post';
                            $WxSyncRelation->inner_id = $this->id;
                            $WxSyncRelation->external_id = $res_arr['post_id'];
                            $WxSyncRelation->status = '1';
                            $WxSyncRelation->save();

                            if(_empty_($sync['uid'])){
                                $user_relation = new WxSyncRelation();
                                $user_relation->domain = $host;
                                $user_relation->type = 'user';
                                $user_relation->inner_id = $this->uid;
                                $user_relation->external_id = $res_arr['user_id'];
                                $user_relation->status = '1';
                                $user_relation->save();
                            }
                            Log::alert('同步成功：'.json_encode($sync, JSON_UNESCAPED_UNICODE));
                        }else{
                            _logger_(__file__, __line__, '同步失败：'.json_encode($sync).'，原因：'.json_encode($res_arr, JSON_UNESCAPED_UNICODE));
                        }
                    }else{
                        _logger_(__file__, __line__, '同步失败：'.json_encode($sync, JSON_UNESCAPED_UNICODE).'，原因：'.$response->body());
                    }
                }else{
                    _logger_(__file__, __line__, '同步失败：'.json_encode($sync, JSON_UNESCAPED_UNICODE).'，原因：'.$response->body());
                }
            }

        }
    }
}
